<?php
include_once 'Xml/ElementXml.php';

class Site_Model_PagSeguro
{
	private $token;
	private $email;
	private $url;
	private $xml;
	
	public function __construct()
	{
		$this->token = 'A1D142F23D7A4BE690CF097BD73E6A12';
		$this->email = "vendas@lemonbrazil.com.br";
		$this->url = "https://ws.pagseguro.uol.com.br/v2/checkout/?email=" . $this->email . "&token=" . $this->token;
		
		$this->mountXML();
		
	}
	
	public function execCheckout()
	{
		$curl = curl_init($this->url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml; charset=ISO-8859-1"));
		curl_setopt($curl, CURLOPT_POSTFIELDS, $this->xml);
		$xml = curl_exec($curl);
		
		if($xml == 'Unauthorized')
		{
			echo 'comunicacao indisponivel no momento';
			exit;//Mantenha essa linha
		}
		curl_close($curl);
		$resultXML = simplexml_load_string($xml);
		
		return $resultXML;
	}
	
	private function mountXML()
	{
		$signup = new Site_Model_Signup();
		$userSession = $signup->getRegisterUser();
		
		$signup = new Application_Model_Db_Signup();
		$userData = $signup->find( $userSession->id )->current();
		
		$order = new Application_Model_Db_Order();
		$itensData = $order->fetchAll( 'buyIdentity = "' . $userSession->buyIdentity . '"' );
		
		$xmlHeader = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>";
		
		$checkout = new ElementXml( 'checkout' );

		$currency = new ElementXml( 'currency' );
		$currency->addElement( 'BRL' );
		$checkout->addElement( $currency );
		
		$redirectURL = new ElementXml( 'redirectURL' );
		$redirectURL->addElement( 'http://lemonbrasil.com.br' );
		$checkout->addElement( $redirectURL );
		
		$items = new ElementXml( 'items' );
		foreach ( $itensData as $key )
		{
			$shippingType = Site_Model_FreteCorreios::convertShippingType($key->shippingType);
			$item = new ElementXml( 'item' );
			
			$id = new ElementXml( 'id' );
			$id->addElement( $key->idOrder );
			$item->addElement( $id );
			
			$description = new ElementXml( 'description' );
			$description->addElement(  $key->name . ' - ' . $key->color . ' - ' . $key->size );
			$item->addElement( $description );
			
			$amount = new ElementXml( 'amount' );
			$amount->addElement( $key->cost );
			$item->addElement( $amount );
			
			$quantity = new ElementXml( 'quantity' );
			$quantity->addElement( '1' );
			$item->addElement( $quantity );
			
			$shippingCost = new ElementXml( 'shippingCost' );
			$shippingCost->addElement( $key->shippingCost );
			$item->addElement( $shippingCost );
			
// 			$weight = new ElementXml( 'weight' );
// 			$weight->addElement( $key->size );
// 			$item->addElement( $weight );
	
			$items->addElement( $item );
		}
		$checkout->addElement( $items );
		
		$reference = new ElementXml( 'reference' );
		$reference->addElement( $userSession->buyIdentity );
		$checkout->addElement( $reference );
		
		$sender = new ElementXml( 'sender' );

		$name = new ElementXml( 'name' );
		$name->addElement( $userData->name );
		$sender->addElement( $name );
		
		$email = new ElementXml( 'email' );
		$email->addElement( $userData->email );
		$sender->addElement( $email );
		
			$phone = new ElementXml( 'phone' );
			
			$areaCode = new ElementXml( 'areaCode' );
			$areaCode->addElement( $userData->areaCode );
			$phone->addElement( $areaCode );
			
			$number = new ElementXml( 'number' );
			$number->addElement( $userData->phoneNumber );
			$phone->addElement( $number );
			
			$sender->addElement( $phone );
		
		$checkout->addElement( $sender );
		
		$shipping = new ElementXml( 'shipping' );
		
		$type = new ElementXml( 'type' );
		$type->addElement( $shippingType );
		$shipping->addElement( $type );
		
		$address = new ElementXml( 'address' );
		
		$street = new ElementXml( 'street' );
		$street->addElement( $userData->street );
		$address->addElement( $street );
		
		$number = new ElementXml( 'number' );
		$number->addElement( $userData->number );
		$address->addElement( $number );
		
		$complement = new ElementXml( 'complement' );
		$complement->addElement( $userData->complement );
		$address->addElement( $complement );
		
		$complement = new ElementXml( 'complement' );
		$complement->addElement( $userData->complement );
		$address->addElement( $complement );
		
		$district = new ElementXml( 'district' );
		$district->addElement( $userData->district );
		$address->addElement( $district );
		
		$postalCode = new ElementXml( 'postalCode' );
		$postalCode->addElement( $userData->postalCode );
		$address->addElement( $postalCode );
		
		$city = new ElementXml( 'city' );
		$city->addElement( $userData->city );
		$address->addElement( $city );
		
		$state = new ElementXml( 'state' );
		$state->addElement( $userData->state );
		$address->addElement( $state );
		
		$country = new ElementXml( 'country' );
		$country->addElement( 'BRA' );
		$address->addElement( $country );
		
		$shipping->addElement( $address );
		$checkout->addElement( $shipping );
		
		$this->xml = $xmlHeader . $checkout->__toString();
	}
	
}



